#include <cstring>
#include <iostream>
#include <string>
using namespace std;

#define MAXSIZE 100

class seqStack
{
public:
    char seqstack[MAXSIZE]={'0'};//初始化为0
    int top;

public:
    seqStack()
    {
        top=-1;
    }

    int push(char ch)
    {
        if(top==MAXSIZE-1)
        {
            cout<<"ERROR:栈满"<<endl;
            return -1;
        }
        top++;
        seqstack[top]=ch;
        return 0;
    }

    char pop()
    {
        if(top==-1)
        {
            cout<<"ERROR:栈空"<<endl;
            return -1;
        }
        char ch=seqstack[top];
        top--;
        return ch;
    }

    int printStack()
    {
        for(int i=0;i<top+1;i++)//需要对top+1不然
        {
            cout<<seqstack[i]<<" ";
        }
        cout<<endl;
        return 0;   
    }

    bool isEmpty()
    {
        return (top==-1);
    }

    int MatchBrackets(char *bracketstring)
    {
        char c,sc;
        for(int i=0;i<strlen(bracketstring);i++)
        {
            c=bracketstring[i];
            if((c=='(')||(c=='[')||(c=='{'))
            {
                push(c);
            }
            if((c==')')||(c==']')||(c=='}'))
            {
                if(isEmpty()==true) 
                    return -2;
                sc=pop();
                if((c==')')&&(sc!='(')) 
                {
                    return -1;
                }
                if((c==']')&&(sc!='[')) 
                {
                    return -1;
                }
                if((c=='}')&&(sc!='{')) 
                {
                    return -1;
                }
            }
        }
        if(isEmpty()==true) return 0;
        else return -3;
        return 0;
    }


};


int main(int argc,char **argv)
{
    
    int samplenum;
    char bracketstring[MAXSIZE];

    cin>>samplenum;
    for (int i = 0; i < samplenum; i++)
    {
        seqStack a;
        cin>>bracketstring;
        cout<<a.MatchBrackets(bracketstring)<<endl;
    }
    return 0;
}